import {reqGoodsInfo , reqAddOrUpdateShopCart} from '@/api/index'
//封装游客身份模块uuid--->生成一个随机字符串（不能在变了）
import {getUUID} from '@/utils/uuid_token';
const state={
  goodInfo: {},
  //游客临时身份
  uuid_token:getUUID()
}
const mutations={
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo;
  },
}
const actions={
  async getGoodInfo({commit},skuId){
    let result= await reqGoodsInfo(skuId)
    if(result.code==200){
      commit("GETGOODINFO", result.data)
    }
  },
  async addOrUpdateShopCart({commit}, { skuId, skuNum }){
    let result=await reqAddOrUpdateShopCart(skuId, skuNum)
    console.log(result);
    if(result.code == 200){
      return "ok"
    }else{
      return Promise.reject(new Error("faile"));
    }
  }
}
const getters={
  //路径导航简化的数据
  categoryView(state) {
    //比如:state.goodInfo初始状态空对象，空对象的categoryView属性值undefined
    //当前计算出的 categoryView属性值至少是一个空对象，假的报错不会有了。
    return state.goodInfo.categoryView || {};
  },
  //简化产品信息的数据
  skuInfo(state) {
    return state.goodInfo.skuInfo || {};
  },
  //产品售卖属性的简化
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || [];
  },
}
export default {
  state,
  mutations,
  actions,
  getters
}
